package fnic.prehand.action;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.io.PrintWriter;
import java.io.Reader;
import java.util.ArrayList;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.log4j.Logger;

import fnic.modesearch.ModeSearch;
import fnic.modesearch.ModeSearch.ModeSearchRuesult;


public class ModeSearchServlet extends HttpServlet{
	private static final Logger LOG = Logger.getLogger(ModeSearchServlet.class);
//	TemplateSearch searcher;
	ModeSearch modesearcher;
	
	public ModeSearchServlet(String idx, String type){
		super();
//		searcher = new TemplateSearch();
		modesearcher = new ModeSearch(idx, type);
	}
	
	protected void doPost(HttpServletRequest request,
			HttpServletResponse response) throws ServletException, IOException {
		InputStream instream = null;
		InputStreamReader isreader = null;
		BufferedReader bufreader = null;
		StringBuilder stringbuilder = new StringBuilder();
		String httpbody;

		try {
			//从http body获取json格式用户请求
			String tempString;
			instream = request.getInputStream();
			isreader = new InputStreamReader(instream, "UTF-8");
			bufreader = new BufferedReader(isreader);
			
			
			while ((tempString = bufreader.readLine()) != null) {
				stringbuilder.append(tempString);
			}
			httpbody = stringbuilder.toString();
			LOG.info("Receive http msg, user define json: " + httpbody);
//			streamReader.close();
//			isr.close();
		} catch (Exception e) {
			//ES模板检索失败 -- 获取Http body失败
			LOG.error("template search get httpbody exception - " + e.getMessage());
			response.setStatus(HttpServletResponse.SC_BAD_REQUEST);
			return;
		}
		
		String result = null; 
		ModeSearchRuesult mresult = new ModeSearchRuesult();
		modesearcher.execuate(httpbody, mresult);
		switch (mresult.getExecode()) {
		case -1:
		case -2:
		case -3:
		case -4:
		case -5:
			LOG.error("mode search error, errorcode=" + mresult.getExecode());
			response.setStatus(HttpServletResponse.SC_BAD_REQUEST);
			break;
		default:
			// 建立输出流，并写入数据
			response.setContentType("application/json;charset=utf-8");
			response.setStatus(HttpServletResponse.SC_OK);
			PrintWriter pw = response.getWriter();
			result = mresult.getResult();
			pw.println(result);
			break;
		}

		bufreader.close();
		isreader.close();
		instream.close();
//		String jsonbody = httpbody.trim();
//		int len= jsonbody.length();
//		if (jsonbody.charAt(0) == '[' && jsonbody.charAt(len-1) == ']'){
//			//手机批量检索
//			// 用户批量请求解析
//			if (searcher.buildBulkTemplate2(jsonbody)) {
//			// 执行检索统计
//				result = searcher.bulkExecuate();
//			}
//			else{
//			//ES模板检索失败 -- 解析查询对象失败
//				LOG.error("template bulk search parse template to obj error" );
//				response.setStatus(HttpServletResponse.SC_BAD_REQUEST);
//				return;
//			}
//		}
//		else if(jsonbody.charAt(0) == '{' && jsonbody.charAt(len-1) == '}'){
//			//Web检索
//			// 用户单个请求解析
//			if(modesearcher.request(streamReader)){
//				modesearcher.execuate();
//				result = modesearcher.getResponse();
//			}else{
//				//ES模板检索失败 -- 解析查询对象失败
//				LOG.error("template single search parse template to obj error" );
//				response.setStatus(HttpServletResponse.SC_BAD_REQUEST);
//			}
//		}
//		else{
//			LOG.error("search req json format illegal !" );
//			response.setStatus(HttpServletResponse.SC_BAD_REQUEST);
//			return;
//		}
	}
	
	protected void doGet(HttpServletRequest request,
			HttpServletResponse response) throws ServletException, IOException {}
}
